package com.sanyue.codingsettlement.model.vo;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.sanyue.codingsettlement.model.entity.Team;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 队伍表视图
 *
 * @author <a href="https://github.com/liyupi">程序员鱼皮</a>
 * @from <a href="https://www.code-nav.cn">编程导航学习圈</a>
 */
@Data
public class TeamVO implements Serializable {

    /**
     * id
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;


    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String description;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tagList;


    /**
     * 队伍性质：0 - 普通 1 - 加密 2 - 私有
     */
    private String status;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 队伍图像
     */
    private String teamAvatar;

    /**
     * 队伍最多人数
     */
    private Integer maxNumber;

    /**
     * 过期时间
     */
    private Date expireTime;

    /**
     * 创建时间
     */
    private Date createTime;


    /**
     * 当前用户是否加入队伍
     */
    private Boolean isJoined;


    /**
     * 当前加入队伍的人数
     */
    private Long hasJoinedNumber;

    private static final long serialVersionUID = 1L;

    /**
     * 创建用户信息
     */
    private UserVO user;

    /**
     * 封装类转对象
     *
     * @param teamVO
     * @return
     */
    public static Team voToObj(TeamVO teamVO) {
        if (teamVO == null) {
            return null;
        }
        Team team = new Team();
        BeanUtils.copyProperties(teamVO, team);
        List<String> tagList = teamVO.getTagList();
        team.setTags(JSONUtil.toJsonStr(tagList));
        return team;
    }

    /**
     * 对象转封装类
     *
     * @param team
     * @return
     */
    public static TeamVO objToVo(Team team) {
        if (team == null) {
            return null;
        }
        TeamVO teamVO = new TeamVO();
        BeanUtils.copyProperties(team, teamVO);
        teamVO.setTagList(JSONUtil.toList(team.getTags(), String.class));
        return teamVO;
    }
}
